package edu.hawaii.duedates.configuration;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import edu.hawaii.duedates.configuration.jaxb.DueDates;
import edu.hawaii.duedates.configuration.jaxb.User;
import edu.hawaii.duedates.configuration.jaxb.Library;

/**
 * Provides access to the contents of the DueDates user log.
 * This user log is stored in ~/.duedates/duedates.xml
 * 
 * @author Tyler Wolff
 */
public class ConfigurationManager {
  
  /** The JAXB DueDates instance read in from the XML file. */
  private DueDates dueDates;

  /**
   * Creates a new ConfigurationManager that reads in the XML DueDates representation.
   * 
   * @param configFile An XML file containing a DueDates user information.
   * @param schemaFile An XSD file containing the XML schema.
   */
  public ConfigurationManager(File configFile, File schemaFile) {
    try {
      JAXBContext jaxbContext = 
        JAXBContext.newInstance(edu.hawaii.duedates.configuration.jaxb.ObjectFactory.class);
      if (configFile.exists()) {
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        SchemaFactory schemaFactory = 
          SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(schemaFile);
        unmarshaller.setSchema(schema);
        this.dueDates = (DueDates) unmarshaller.unmarshal(configFile);
      }
    }
    catch (Exception e) {
      throw new RuntimeException("Error in Due Dates ConfigurationManager", e);
    }
  }
  
  /**
   * Create a new ConfigurationManager that provides access to the DueDates 
   * in ~/.duedates/duedates.xml.
   */
  public ConfigurationManager() {
    this(new File(System.getProperty("user.home") + "/.duedates/duedates.xml"),
        new File(System.getProperty("user.home") + "/.duedates/duedates.xsd"));
  }
  
  /**
   * Returns a DueDatesConfiguration constructed from the default XML DueDates. 
   * If no default DueDates exists, an empty DueDates is returned. 
   * @return A DueDatesConfiguration object with all the data. 
   */
  public DueDatesConfiguration getDueDates() {
    
    DueDatesConfiguration config = new DueDatesConfiguration();
   
    if (this.dueDates != null) {
      
      config.setSmtpServer(this.dueDates.getSmtpServer());
      
      // Goes through and adds each user with library info
      for (User user : this.dueDates.getUsers().getUser()) {
        
        edu.hawaii.duedates.configuration.User newUser = new edu.hawaii.duedates.configuration.User
          (user.getName(), user.getEmail(), user.getPassword());
        
        for (Library library : user.getLibrary()) {
          
          if (library.getName().equals("University of Hawaii")) {
            newUser.setUhManoa("-uhm " + library.getUsername() + " " + library.getPassword());
          }
          if (library.getName().equals("Hawaii State Library")) {
            newUser.setHawaiiStateLib("-hsl " + library.getUsername() + " " + 
                library.getPassword());
          }
        }
        
        config.addUser(newUser);
        
      } // end for loop       
    }
    return config;
  }
}
